import { color } from "../config"
import { curveVector } from "../config/curve"
import * as THREE from "three"
import vertexShader from "../shaders/curve/vertex.glsl"
import fragmentShader from "../shaders/curve/frag.glsl"
export class MyCurve {
    scene: THREE.Scene
    time: THREE.IUniform
    constructor(scene: THREE.Scene, time: THREE.IUniform) {
        this.scene = scene
        this.time = time
        this.init()
    }
    init() {
        curveVector.map(vector3 => {
            this.creatCurve(vector3)
        })

    }
    creatCurve(vector3: curveVector3) {
        const source = new THREE.Vector3(vector3.start.x, vector3.start.y, vector3.start.z)
        const target = new THREE.Vector3(vector3.end.x, vector3.end.y, vector3.end.z)

        //中心点
        const center = source.clone().lerp(target, 0.5)
        center.y = 200

        // 根据距离获取顶点数量
        let total = source.distanceTo(target)
        const curve = new THREE.QuadraticBezierCurve3(
            source, center, target
        )

        //获取顶点数量
        const points = curve.getSpacedPoints(total)
        const geometry = new THREE.BufferGeometry()
        let F32Points: number[] = []
        let pointsIndex: number[] = []
        points.map((v, index) => {
            F32Points.push(v.x, v.y, v.z)
            pointsIndex.push(index)
        })
        geometry.setAttribute("position", new THREE.BufferAttribute(new Float32Array(F32Points), 3))
        geometry.setAttribute("positionIndex", new THREE.BufferAttribute(new Float32Array(pointsIndex), 1))
        const material = new THREE.ShaderMaterial({
            vertexShader,
            fragmentShader,
            linewidth: 5.0,
            uniforms: {
                u_color: {
                    value: new THREE.Color(color.fly)
                },
                f_total: {
                    value: total
                },
                f_time: this.time,
                f_range: {
                    value: 100.0
                },
                f_size: {
                    value: 5.0
                },
                u_opacity: {
                    value: 0.5
                },
                u_speed: {
                    value: 30
                }
            },
            transparent: true,
            side: THREE.DoubleSide,
            // depthTest: false
        })
        const point = new THREE.Points(geometry, material)
        point.name='curve'

        this.scene.add(point)
    }
}